﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;

namespace Agile.Engine.View
{
    public interface IViewProvider
    {
        /// <summary>
        /// 视图对象
        /// </summary>
        IView View { get; set; }

        Agile.Engine.Data.DataContext DataContext { get; set; }
        /// <summary>
        /// 创建视图
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        T CreateView<T>(ViewState viewState, System.Collections.Hashtable args)
            where T : class, IView, new();

        /// <summary>
        /// 初始化数据模型
        /// </summary>
        /// <returns></returns>
        Hashtable InitializeViewModel(System.Collections.Hashtable param);

        /// <summary>
        /// 执行查询操作
        /// </summary>
        /// <returns></returns>
        Hashtable ExecuteQuery(object param);

        /// <summary>
        /// 执行更新操作
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        bool ExecuteUpdate(Hashtable viewModel);

        /// <summary>
        /// 执行更新操作
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        bool ExecuteUpdateXtra(Hashtable viewModel);


        /// <summary>
        /// 数据表名
        /// </summary>
        string DataTableName { get; set; }
        /// <summary>
        /// 数据表拓展信息
        /// </summary>
        string DataXtraIndex { get; set; }

        /// <summary>
        /// 扩展数据初始化
        /// </summary>
        /// <param name="xtra"></param>
        void InitializeDataXtra(System.Collections.Hashtable xtra);

        ViewState ViewState { get; set; }

        /// <summary>
        /// 执行验证
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        bool ExecuteValidate(Hashtable viewModel);

        string Message { get; set; }
    }


    public class ViewProvider : IViewProvider
    {
        public IView View { get; set; }
        public Data.DataContext DataContext { get; set; }
        public string DataTableName { get; set; }
        public string DataXtraIndex { get; set; }
        public ViewState ViewState { get; set; }
        public string Message { get; set; }
        private object syncRoot = new object();
        public virtual void InitializeDataXtra(System.Collections.Hashtable xtra) { }

        /// <summary>
        /// 执行步骤：
        /// 1，重设链接
        /// 2，异步取数
        /// 3，创建View
        /// 4，设置关键字
        /// 5，设置viewstate
        /// 6，同步数据
        /// 7，viewUI初始化
        /// 8，数据绑定
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="viewState"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public T CreateView<T>(ViewState viewState, System.Collections.Hashtable args)
            where T : class,IView, new()
        {
            ViewState = viewState;
            ResetConnectionString();
            ///异步
            var asyn = new Func<System.Collections.Hashtable>(
                () =>
                {
                    var vm = InitializeViewModel(args);
                    InitializeDataXtra(vm);
                    return vm;
                });
            var callback = new Action<IAsyncResult>((a) => { });
            IAsyncResult ar;

            ///解析参数
            ParseCreateArgs(args);
            ar = asyn.BeginInvoke(new AsyncCallback(callback), null);
            ///视图实例
            View = new T();// Utils.ReflectUtils.Instance(typeof(T).AssemblyQualifiedName) as IView;
            ///创建View
            View.ViewState = viewState;
            View.ViewProvider = this;
            if (args != null && args.ContainsKey("KeyWords"))
                View.KeyWords = args["KeyWords"].ToString();

            ///同步
            lock (syncRoot)
            {
                ///视图数据
                View.ViewModel = asyn.EndInvoke(ar);
                ///画面控件初始化
                View.InitializeViewUI();
                ///数据绑定
                View.InitializeBinding();
            }
            return View as T;
        }

        /// <summary>
        /// 解析参数
        /// </summary>
        /// <param name="args"></param>
        protected virtual bool ParseCreateArgs(System.Collections.Hashtable args)
        {
            return true;
        }

        /// <summary>
        /// 异步取数
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public virtual System.Collections.Hashtable InitializeViewModel(Hashtable args)
        {
            Hashtable m = args ?? new Hashtable();

            switch (ViewState)
            {
                ///新增
                case Agile.Engine.View.ViewState.AddNew:
                    {
                        var a = new Agile.Engine.Data.DataAccess() { Context = DataContext, TableName = DataTableName };
                        m["viewData"] = a.GetNewData();
                    }
                    break;
                case Engine.View.ViewState.View:
                    {
                        var a = new Agile.Engine.Data.DataAccess() { Context = DataContext, TableName = DataTableName };
                        m["viewData"] = a.GetData();
                    }
                    break;

            }
            return m;

        }

        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public virtual System.Collections.Hashtable ExecuteQuery(object param)
        {
            return new System.Collections.Hashtable();
        }

        /// <summary>
        /// 执行更新
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public virtual bool ExecuteUpdate(System.Collections.Hashtable viewModel)
        {
            return true;
        }

        /// <summary>
        /// 重设字符串
        /// </summary>
        protected virtual void ResetConnectionString() { }

        /// <summary>
        /// 更新扩展信息
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public virtual bool ExecuteUpdateXtra(Hashtable viewModel)
        {
            return true;
        }

        public virtual bool ExecuteValidate(Hashtable viewModel)
        {
            return true;
        }

    }

}
